package com.jvyou.app.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.common.exception.AppException;
import com.jvyou.app.ds.checker.LessonStatusChecker;
import com.jvyou.app.ds.domain.entity.Lesson;
import com.jvyou.app.ds.domain.entity.LessonStudent;
import com.jvyou.app.ds.domain.entity.LicenseType;
import com.jvyou.app.ds.domain.entity.Student;
import com.jvyou.app.ds.mapper.LessonMapper;
import com.jvyou.app.ds.mapper.LessonStudentMapper;
import com.jvyou.app.ds.mapper.LicenseTypeMapper;
import com.jvyou.app.ds.mapper.StudentMapper;
import com.jvyou.app.ds.service.LessonStudentService;
import com.jvyou.app.ds.utils.login.LoginContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 课时学员关联表(LessonStudent)表服务实现类
 *
 * @author 橘柚
 * @since 2025-04-17 17:08:42
 */
@Service("lessonStudentService")
@RequiredArgsConstructor
public class LessonStudentServiceImpl extends ServiceImpl<LessonStudentMapper, LessonStudent> implements LessonStudentService {

    private final LessonMapper lessonMapper;
    private final StudentMapper studentMapper;
    private final LicenseTypeMapper licenseTypeMapper;

    @Override
    public Boolean signIn(Long lessonId) {
        LambdaQueryWrapper<LessonStudent> ew = new LambdaQueryWrapper<>();
        ew.eq(LessonStudent::getLessonId, lessonId);
        ew.eq(LessonStudent::getStudentId, LoginContext.getUserId());
        return null;
    }

    @Transactional
    @Override
    public Boolean reserve(Long lessonId) {
        Lesson lesson = lessonMapper.selectById(lessonId);
        if (lesson == null) {
            throw new AppException(HttpCodeEnum.DATA_NOT_EXIST, "预约失败，查询不到课程信息");
        }
        if (lesson.getCurrentStudents() >= lesson.getMaxStudents()) {
            throw new AppException(HttpCodeEnum.DATA_NOT_EXIST, "预约失败，该课程已满员");
        }
        Long userId = LoginContext.getUserId();
        Student student = studentMapper.getByUser(userId);
        if (student == null) {
            throw new AppException(HttpCodeEnum.DATA_NOT_EXIST, "预约失败，查询不到学员信息");
        }
        // 判断驾照类型
        LicenseType licenseType = licenseTypeMapper.selectById(lesson.getLicenseTypeId());
        if (licenseType == null) {
            throw new AppException(HttpCodeEnum.DATA_NOT_EXIST, "预约失败，课时信息异常，找不到匹配的驾照类型");
        }
        if (!student.getEnrollmentType().equals(licenseType.getLicenseCode())) {
            throw new AppException(HttpCodeEnum.DATA_NOT_EXIST, "预约失败，您的驾照类型与课时不匹配");
        }
        // 判断是否已预约
        LambdaQueryWrapper<LessonStudent> ew = new LambdaQueryWrapper<>();
        ew.eq(LessonStudent::getLessonId, lessonId);
        ew.eq(LessonStudent::getStudentId, student.getId());
        LessonStudent lessonStudent = getOne(ew);
        if (lessonStudent != null && (lessonStudent.getStatus().equals("1") || lessonStudent.getStatus().equals("2"))) {
            throw new AppException(HttpCodeEnum.DATA_NOT_EXIST, "预约失败，您已预约过该课程");
        }
        // 判断是否可预约
        String status = LessonStatusChecker.checkStatus(lesson);
        if (!status.equals(LessonStatusChecker.NORMAL)) {
            throw new AppException(HttpCodeEnum.DATA_NOT_EXIST, LessonStatusChecker.getErrorMessage(status, "预约失败"));
        }

        // 如果已经取消过的，修改状态为未签到
        if (lessonStudent != null && lessonStudent.getStatus().equals("3")) {
            lessonStudent.setStatus("1");
            lessonStudent.setEvaluationScore(0);
            lessonStudent.setEvaluationContent("");
            updateById(lessonStudent);
        } else {
            lessonStudent = new LessonStudent();
            lessonStudent.setLessonId(lessonId);
            lessonStudent.setStudentId(student.getId());
            lessonStudent.setStatus("1");
            lessonStudent.setEvaluationScore(0);
            lessonStudent.setEvaluationContent("");
            save(lessonStudent);
        }
        lesson.setCurrentStudents(lesson.getCurrentStudents() + 1);
        if (lesson.getCurrentStudents() >= lesson.getMaxStudents()) {
            lesson.setStatus("2");
        }
        lessonMapper.updateById(lesson);
        return true;
    }
}
